package com.pdstudio.carrecom.ui.fragment.more;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.http.RequestParams;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.app.UIHelper;
import com.pdstudio.carrecom.bean.PhoneValideCode;
import com.pdstudio.carrecom.ui.activity.account.ActivityGetPassword;
import com.pdstudio.carrecom.ui.activity.account.ActivityGoRegister;
import com.pdstudio.carrecom.ui.activity.more.ActivityValidePhoneAg;
import com.pdstudio.carrecom.ui.fragment.account.FragmentRegisterS2;

public class FragmentValidePhoneAg1 extends Fragment {

	private View _view;
	private Activity _context;

	private Button mGetValideCode, mNextStep;
	private EditText mValideCode, mPhone;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		_view = inflater.inflate(R.layout.fragment_more_validephoneag1,
				container, false);
		_context = getActivity();
		initialView();
		return _view;
	}

	public void initialView() {
		mGetValideCode = (Button) _view.findViewById(R.id.more_vp_ag_getcode);
		mGetValideCode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					if (mPhone.getText().toString().equals("")
							|| mPhone.getText() == null) {
						UIHelper.ToastMessage(_context, "手机号不能为空！");
					} else {
						((ActivityValidePhoneAg) _context).mOldPhone = mPhone
								.getText().toString();
						HttpExecuteJson http = new HttpExecuteJson(_context,
								mLoginListener);
						RequestParams rps = new RequestParams();
						rps.addBodyParameter("id",
								AppContext.getInstance()._userId + "");
						http.post(ServiceHelper.AgValidePhone1 + "/"
								+ mPhone.getText().toString(), rps);
					}
				} catch (Exception e) {
					e.printStackTrace();
					UIHelper.ToastMessage(_context,
							"获取验证码出现错误：" + e.getMessage());
				}

			}
		});
		mNextStep = (Button) _view.findViewById(R.id.more_vp_ag_next);
		mNextStep.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mValideCode.getText() == null
						|| mValideCode.getText().equals("")) {
					UIHelper.ToastMessage(_context, "请先获取验证码");
				} else {
					ActivityValidePhoneAg agr = (ActivityValidePhoneAg) _context;
					agr.openFragment(new FragmentValidePhoneAg2());
				}
			}
		});
		mValideCode = (EditText) _view.findViewById(R.id.more_vp_ag_code);
		mValideCode.addTextChangedListener(codeTextWatcher);
		mPhone = (EditText) _view.findViewById(R.id.more_vp_ag_phone);
		mPhone.addTextChangedListener(phoneTextWatcher);
	}

	TextWatcher phoneTextWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if (mPhone.getText() != null && mPhone.getText().toString() != "") {
				mValideCode.setEnabled(true);
			}

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			if (mPhone.getText() != null && mPhone.getText().toString() != "") {
				mValideCode.setEnabled(true);
			} else {
				mValideCode.setEnabled(false);
			}

		}

		@Override
		public void afterTextChanged(Editable s) {
			if (mPhone.getText() != null && mPhone.getText().toString() != "") {
				mValideCode.setEnabled(true);
			} else {
				mValideCode.setEnabled(false);
			}

		}
	};

	TextWatcher codeTextWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			if (mValideCode.getText() != null && mValideCode.getText().toString() != "") {
				mNextStep.setEnabled(true);
			}

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			if (mValideCode.getText() != null && mValideCode.getText().toString() != "") {
				mNextStep.setEnabled(true);
			} else {
				mNextStep.setEnabled(false);
			}

		}

		@Override
		public void afterTextChanged(Editable s) {
			if (mValideCode.getText() != null && mValideCode.getText().toString() != "") {
				mNextStep.setEnabled(true);
			} else {
				mNextStep.setEnabled(false);
			}

		}
	};

	private void DoJson(String json) {
		PhoneValideCode edv = null;
		try {
			edv = new Gson().fromJson(json, new TypeToken<PhoneValideCode>() {
			}.getType());
			if ((edv != null)
					&& (edv.msg.equals(ServiceHelper.msg_code_sucess))) {
				// 原本在initview的代码放在这下面
				try {
					mValideCode.setText(edv.code);

				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				UIHelper.ToastMessage(_context, "获取验证码失败");

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private httpReturnJson mLoginListener = new httpReturnJson() {
		@Override
		public void onFailure(int error, String msg) {
			UIHelper.ToastMessage(_context, "获取验证码失败" + msg);
		}

		@Override
		public void onCancel() {
			// TODO Auto-generated method stub
			UIHelper.ToastMessage(_context, "获取验证码失败");
		}

		// 获取到饲料配方类型成功
		public void onSuccess(String result) {
			// TODO Auto-generated method stub
			// UIHelper.ToastMessage(_context,"总条数=" + result + "");
			DoJson(result);
		}
	};

}
